﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：ConsumablesController
// 文件功能描述：零星物品入库数据控制层
//
// 
// 创建标识：2012-01-14 zhou
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    [WaiGaoQiaoWeb.FilterSession.AuthorizeAttribute]
    public class ConsumablesController : Controller
    {
        //
        // GET: /Consumables/

        public ActionResult ConsumablesIndex()
        {          
            return View();
        }
        protected InstockSporadicInfo instockSp = new InstockSporadicInfo(true);
        private Con_GoodsInfo conInfo = new Con_GoodsInfo(true);
        /// 读取食材入库明细
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetDetailList(PageVM pvm)
        {
            #region 注释代码
            //string temp = string.Empty;
            //if (!string.IsNullOrEmpty(pvm.query))
            //{
            //    string[] condition = pvm.query.Split(',');
            //    if (!string.IsNullOrEmpty(condition[0]) && condition[0] == "0")
            //    {

            //        temp += condition[1];
            //    }
            //    else
            //    {

            //        if (!string.IsNullOrWhiteSpace(condition[0]))
            //        {
            //            temp += "AND InstockDt='" + condition[0] + "'";
            //        }
            //        if (!string.IsNullOrWhiteSpace(condition[1]))
            //        {
            //            temp += " AND Goods_ID='" + condition[1] + "'";
            //        }
            //    }
            //}
            //else
            //{

            //    temp += "  AND OrderNo = ''";
            //}
            #endregion
            if (string.IsNullOrEmpty(pvm.query))
            {
                pvm.query = "and OrderNo = ''";
            }
            string condition = pvm.query;//"and OrderNo = '" + pvm.query + "'";
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = instockSp.GetRowCount(condition);

            IList<InstockSporadicInfo> splist = instockSp.GetInstockGroup(pvm.rp, pvm.page, condition);

            List<string> v;
            foreach (InstockSporadicInfo ida in splist)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.InstockDt.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo));
                v.Add(Con_GoodsInfo.GetGoodsNameByID(ida.Goods_ID));
                v.Add(ida.Count.ToString());
                v.Add(ida.UserMan);
                v.Add(ida.Remark);
                v.Add(ida.RecordDateTime.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo));
                v.Add(ida.OperatorID.ToString());
                v.Add(ida.OrderNo);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);

        }
       
        /// 读取食材入库明细
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetDetailListByOrderNo(PageVM pvm)
        {
            string condition = "and OrderNo = '" + pvm.query + "'";
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = instockSp.GetRowCount(condition);

            IList<InstockSporadicInfo> splist = instockSp.GetInstockGroup(pvm.rp, pvm.page, condition);

            List<string> v;
            foreach (InstockSporadicInfo ida in splist)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.InstockDt.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo));
                v.Add(Con_GoodsInfo.GetGoodsNameByID(ida.Goods_ID));
                v.Add(ida.Count.ToString());
                v.Add(ida.UserMan);
                v.Add(ida.Remark);
                v.Add(ida.RecordDateTime.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo));
                v.Add(ida.OperatorID.ToString());
                v.Add(ida.OrderNo);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);

        }


        //*******************************************************
        //  新建食材明细
        //*******************************************************
        public ActionResult AddConsumablesDetail(int? id)
        {
            ConsumablesDetailVM idvm = new ConsumablesDetailVM();
            if (id == null)
            {
              
                idvm.ID = 0;
                idvm.Count = 0;
                idvm.RecordDateTime = DateTime.Now.ToString("yyyy-MM-dd");
                idvm.OperatorID = ((LogOnModel)System.Web.HttpContext.Current.Session["user"]).LogName;
                idvm.OrderNo = instockSp.GetInstockSporadicOrderNo();
                idvm.InstockDt1 = DateTime.Now.ToString("yyyy-MM-dd");
            }
            else
            {

                InstockSporadicInfo idinfo = instockSp.GetInstockGroup(Convert.ToInt32(id));
                Mapper.DynamicMap(idinfo, idvm);
                idvm.InstockDt1 = idinfo.InstockDt.ToString("yyyy-MM-dd");
                idvm.RecordDateTime = idinfo.RecordDateTime.ToString("yyyy-MM-dd");
            }
            return View(idvm);
        }
      
        [HttpPost]
        public JsonResult SaveInstockDetail(ConsumablesDetailVM cdvm)
        {
            InstockSporadicInfo info = new InstockSporadicInfo();
            info.ID = cdvm.ID;
            info.Count = cdvm.Count;
            info.InstockDt = Convert.ToDateTime(cdvm.InstockDt1);
            info.Goods_ID = cdvm.Goods_ID;
            info.UserMan = cdvm.UserMan;
            info.Remark = cdvm.Remark;
            info.RecordDateTime = Convert.ToDateTime(cdvm.RecordDateTime);
            info.OperatorID = cdvm.OperatorID;
            info.OrderNo = cdvm.OrderNo;
            if (ModelState.IsValid)
            {
                instockSp.SaveSundries(info);
            }
            return Json(true);
        }
        [HttpPost]
        public JsonResult Delete(int id)
        {
            int rowcount = instockSp.DeleteInstockGroup(id);
            return Json(rowcount);
        }
     }
}
